<?php

class Event {

    public $id;
    public $raw;
    public $nr;
    public $distance;
    public $stroke;
    public $relay;
    public $age;
    public $low_age;
    public $high_age;
    public $gender;
    public $competition_id;
    public $type;
    public $date;
    //not database fields
    public $finaBaseTime;

    function __construct($raw = '', $nr = '', $distance = '', $stroke = '', $relay = false, $age = '', $low_age = 0, $high_age = 109, $gender = '', $competition_id = '', $type = '', $date = '') {
        $this->raw = $raw;
        $this->nr = $nr;
        $this->distance = $distance;
        $this->stroke = $stroke;
        $this->relay = $relay;
        $this->age = $age;
        $this->low_age = $low_age;
        $this->high_age = $high_age;
        $this->gender = $gender;
        $this->competition_id = $competition_id;
        $this->type = $type;
        $this->date = $date;
    }

    /**
     * Load Event from db by ID.
     * @param int $id
     * @return Event
     */
    public static function fromID($id) {
        $instance = new self();
        $instance->loadByID($id);
        return $instance;
    }

    /**
     * Load Event from database row.
     * @param array $row
     * @return Event
     */
    public static function fromRow(array $row) {
        $instance = new self();
        $instance->fill($row);
        return $instance;
    }

    private function loadByID($id) {
        $result = mysql_query("SELECT `id`, `raw`, `nr`, `distance`, `stroke`, `relay`, `age`, `low_age`, `high_age`, `gender`, `competition_id`, `type`, `date` FROM `events` WHERE `id`='$id'");
        if (mysql_num_rows($result) == 0) {
            return null;
        }
        $row = mysql_fetch_assoc($result);
        $this->fill($row);
    }

    private function fill(array $row) {
        if (isset($row['id']))
        $this->id = $row['id'];
        if (isset($row['raw']))
        $this->raw = $row['raw'];
        if (isset($row['nr']))
        $this->nr = $row['nr'];
        if (isset($row['distance']))
        $this->distance = $row['distance'];
        if (isset($row['stroke']))
        $this->stroke = $row['stroke'];
        if (isset($row['relay']))
        $this->relay = $row['relay'];
        if (isset($row['age']))
        $this->age = $row['age'];
        if (isset($row['low_age']))
        $this->low_age = $row['low_age'];
        if (isset($row['high_age']))
        $this->high_age = $row['high_age'];
        if (isset($row['gender']))
        $this->gender = $row['gender'];
        if (isset($row['competition_id']))
        $this->competition_id = $row['competition_id'];
        if (isset($row['type']))
        $this->type = $row['type'];
        if (isset($row['date']))
        $this->date = $row['date'];
    }

    public function toArray() {
        return array($this->raw, $this->nr, $this->distance, $this->stroke, $this->relay, $this->age, $this->low_age, $this->high_age, $this->gender, $this->competition_id, $this->type, $this->date);
    }

    public function getID() {
        return $this->id;
    }

    public function getRaw() {
        return $this->raw;
    }

    public function setRaw($raw) {
        $this->raw = $raw;
    }

    public function getNr() {
        return $this->nr;
    }

    public function setNr($nr) {
        $this->nr = $nr;
    }

    public function getDistance() {
        return $this->distance;
    }

    public function setDistance($distance) {
        $this->distance = $distance;
    }

    public function getStroke() {
        return $this->stroke;
    }

    public function setStroke($stroke) {
        $this->stroke = $stroke;
    }

    public function isRelay() {
        return $this->relay;
    }

    public function setIsRelay($relay) {
        $this->relay = $relay;
    }

    public function getAge() {
        return $this->age;
    }

    public function setAge($age) {
        $this->age = $age;
    }
    
    public function getLowAge() {
        return $this->low_age;
    }

    public function setLowAge($low_age) {
        $this->low_age = $low_age;
    }

    public function getHighAge() {
        return $this->high_age;
    }

    public function setHighAge($high_age) {
        $this->high_age = $high_age;
    }

    
    public function getGender() {
        return $this->gender;
    }

    public function setGender($gender) {
        $this->gender = $gender;
    }

    public function getCompetitionID() {
        return $this->competition_id;
    }

    public function setCompetitionID($competition_id) {
        $this->competition_id = $competition_id;
    }

    public function getType() {
        return $this->type;
    }

    public function setType($type) {
        $this->type = $type;
    }

    public function __toString() {
        return "raw: $this->raw, nr: $this->nr, stroke: $this->stroke, distance: $this->distance, relay: $this->relay<br>";
    }

    public function toTableRow() {
        return "<tr><td>$this->raw</td><td>$this->nr</td><td>$this->stroke</td><td>$this->distance</td></tr>";
    }

    public function toTableColumns() {
        return "<td>$this->raw</td><td>$this->nr</td><td>$this->stroke</td><td>$this->distance</td>";
    }

    public function getFinaBaseTime() {
        return $this->finaBaseTime;
    }

    public function setFinaBaseTime($pool, $start_date) {
        $distance = $this->distance;
        if ($distance <= 25)
            return 0;
        if ($this->relay == 1)
            $distance /= 4;
        
        $year = date('Y', strtotime($start_date));
        $result = mysql_query("SELECT `time_ms` FROM `fina` WHERE `year`<=$year and `pool`=$pool AND UPPER(gender)=UPPER('$this->gender') AND 
        `relay`=$this->relay and `distance`=$distance and UPPER(stroke)=UPPER('$this->stroke') ORDER BY `year` DESC LIMIT 1") or die(mysql_error());
        if (mysql_num_rows($result) == 0) {
            return 0;
        }
        $this->finaBaseTime = mysql_result($result, 0);
    }

    public function toStringEst() {
        return $this->distance . ' m ' . Utils::stroke_to_est($this->stroke)
                . ' ' . Utils::gender_to_est($this->gender) . ($this->relay ? ' teateujumine' : '')
                . ($this->age == '' ? '' : ' vanus ' . $this->age);
    }
    
    public function getDate() {
        return $this->date;
    }

    public function setDate($date) {
        $this->date = $date;
    }

    
    public static function fromRawData($raw) {
        $instance = new self();
        $raw = strtolower($raw);

        $instance->raw = $raw;

        $nr_matches = null;
        preg_match_all('/(?<=event )([0-9]+)/i', $raw, $nr_matches);
        $instance->nr = $nr_matches[0][0];

        $distance_matches = null;
        preg_match_all('/([0-9]+)/i', $raw, $distance_matches);

        //make sure to get last value, so Event # will not be considered as distance
        foreach ($distance_matches[0] as $number) {
            if ($number % 25 == 0) {
                $instance->distance = $number;
            }
        }
        $stroke = 'tuvastamata';

        if (strpos($raw, 'fly') !== false) {
            $stroke = 'fly';
        } else if (strpos($raw, 'back') !== false) {
            $stroke = 'back';
        } else if (strpos($raw, 'breast') !== false) {
            $stroke = 'breast';
        } else if (strpos($raw, 'free') !== false) {
            $stroke = 'free';
        } else if (strpos($raw, 'im') !== false ||
                strpos($raw, 'medley') !== false) {
            $stroke = 'medley';
        }
        $instance->stroke = $stroke;
        $instance->relay = 0;
        if (strpos($raw, 'relay') !== false) {
            $instance->relay = 1;
        }

        $age_matches = null;
        $pattern = '/(?<=mixed |girls |boys |women |men )([0-9-]+)( & under| & over)?/i';
        $n = preg_match_all($pattern, $raw, $age_matches);
        if ($n === 0) {
            $instance->age = "";
        } else {
            $instance->age = ($age_matches[0][0] % 25 == 0 ? "" : $age_matches[0][0]);
        }

        //for relay, short of miXed
        $gender = 'X';
        if (strpos($raw, 'girls') !== false || strpos($raw, 'women') !== false) {
            $gender = 'F';
        } else if (strpos($raw, 'boys') !== false || strpos($raw, 'men') !== false) {
            $gender = 'M';
        }

        $instance->gender = $gender;

        $type = 'N';
        if (strpos($raw, 'semifinal') !== false) {
            $type = 'SF';
        } else if (strpos($raw, 'swim-off') !== false) {
            $type = 'SO';
        } else if (strpos($raw, 'final') !== false) {
            $type = 'F';
        } else if (strpos($raw, 'preliminaries') !== false) {
            $type = 'P';
        }
        $instance->type = $type;

        return $instance;
    }
    

}

?>
